package com.qq.client.view;

import java.awt.BorderLayout;
import com.qq.client.tools.*; 
import com.qq.common.Massage;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class QqfriendList extends JFrame implements ActionListener,MouseListener{
	//第一章卡片  我的好友
    JPanel jphy1,jphy2,jphy3;
    JButton jphy_jb1,jphy_jb2,jphy_jb3;
    //用于切换的JScrollPane
    JScrollPane jsp1;
    //第二张卡片()陌生人
    JPanel jpmsr1,jpmsr2,jpmsr3;
    JButton jpmsr_jb1,jpmsr_jb2,jpmsr_jb3;
    //用于切换的JScrollPane
    JScrollPane jsp2;
    
    //把这个JFrame设置成GardLayout（）
    JLabel []jb1s;
    CardLayout cl;
    String wodeId;
	public static void main(String[] args) {
	//	QqfriendList qqfl=new QqfriendList(String wodeId);
	}
	//更新好友列表的方法。
	
	public void updateFrirend(Massage m){
		String onLine[]=m.getCon().split(" ");
		for(int i=0;i<onLine.length;i++){
			jb1s[Integer.parseInt(onLine[i])-1].setEnabled(true);
		}
	}
	
    public QqfriendList(String wodeId){
    	//处理第一章卡片
    	this.wodeId=wodeId;
    	jphy_jb1=new JButton("我的好友");
    	jphy_jb2=new JButton("陌生人");
    	jphy_jb2.addActionListener(this);
    	jphy_jb3=new JButton("黑名单");
    	jphy1=new JPanel(new BorderLayout());
    	//要在这里加入从数据库返回的好友，而不是固定的好友。
    	//只要在数据库中，返回一个String数组，在这创建，接收。int一个变量，得到长度x，将50改成x，
    	//将i+1改成String【i】。
    	jphy2=new JPanel(new GridLayout(50,1,4,4));
    	jb1s =new JLabel[50];
    	for(int i=0;i<jb1s.length;i++){
    		jb1s[i]=new JLabel(i+1+"",new ImageIcon("image/mm.jpg"),JLabel.LEFT);
    		jb1s[i].setEnabled(false);
    		if(jb1s[i].getText().equals(wodeId)){
    			jb1s[i].setEnabled(true);
    		}
    		//给每一个都加如，鼠标响应
    		jb1s[i].addMouseListener(this);
    		jphy2.add(jb1s[i]);
    	}
    	jphy3=new JPanel(new GridLayout(2,1));
    	
    	//把两个按钮加入
    	jphy3.add(jphy_jb2);
    	jphy3.add(jphy_jb3);
    	jsp1=new JScrollPane(jphy2);
    	//jphy1
    	jphy1.add(jphy_jb1,"North");
    	jphy1.add(jsp1,"Center");
    	jphy1.add(jphy3,"South");
    	this.add(jphy1,"Center");
    	this.setSize(140,500);
    	this.setVisible(true);
    	
    	
    	
    	//处理第二张卡片(陌生人)
       	jpmsr_jb1=new JButton("我的好友");
       	jpmsr_jb1.addActionListener(this);
    	jpmsr_jb2=new JButton("陌生人");
    	jpmsr_jb3=new JButton("黑名单");
    	jpmsr1=new JPanel(new BorderLayout());
    	jpmsr2=new JPanel(new GridLayout(20,1,4,4));
    	JLabel []jb1s2=new JLabel[20];
    	for(int i=0;i<jb1s2.length;i++){
    		jb1s2[i]=new JLabel(i+1+"",new ImageIcon("image/mm.jpg"),JLabel.LEFT);
    		jb1s2[i].addMouseListener(this);
    		jpmsr2.add(jb1s2[i]);
    	}
    	jpmsr3=new JPanel(new GridLayout(2,1));
    	
    	//把两个按钮加入
    	jpmsr3.add(jpmsr_jb1);
    	jpmsr3.add(jpmsr_jb2);
    	jsp2=new JScrollPane(jpmsr2);
    	//处理
    	jpmsr1.add(jpmsr3,"North");
    	jpmsr1.add(jsp2,"Center");
    	jpmsr1.add(jpmsr_jb3,"South");
    	
    	cl=new CardLayout();
    	this.setTitle(wodeId);
    	this.setLayout(cl);
    	this.add(jphy1,"1");
    	this.add(jpmsr1,"2");
    	
    	
    }
	public void actionPerformed(ActionEvent agr0) {
	if(agr0.getSource()==jphy_jb2){
			cl.show(this.getContentPane(),"2");
	}
	else if(agr0.getSource()==jpmsr_jb1){
		cl.show(this.getContentPane(),"1");
	}
	}
    
	public void mouseClicked(MouseEvent e) {
		//双击事件，得到好友编号
		if(e.getClickCount()==2){
			String haoyou=((JLabel)e.getSource()).getText();
			QqChat qqchat =new QqChat(wodeId,haoyou);
			//吧聊天界面加入到管理类中。
			ManagerQqChat.addQqchat(this.wodeId+" "+haoyou, qqchat);
		}
	}
	public void mouseEntered(MouseEvent e) {
		JLabel jl=(JLabel)e.getSource();
		jl.setForeground(Color.red);
	}
	public void mouseExited(MouseEvent e) {
		JLabel jl=(JLabel)e.getSource();
		jl.setForeground(Color.black);
	}
	public void mousePressed(MouseEvent e) {
		
	}
	public void mouseReleased(MouseEvent e) {
		
	}
    
	
}
